import 'package:hxdj/common/base_page/base_page.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'package:hxdj/home/home_viewmodel.dart';

class SelectTechnicianViewModel extends BaseViewModel {
 
  List<NearbyTechnicianModel> dataList = [];

  int page = 1;
  int limit = 50; //MyConstant.pageSize;

  bool isHave = true;

  @override
  requestData(
      {Function(dynamic)? onSuccess,
      Function(dynamic)? onError,
      bool isFresh = false,
      bool isCleanDate = false,
      String tId = ""}) async {
    if (isFresh) {
      setState(ViewState.loading);
    }

    if (isCleanDate) {
      dataList.clear();
      page = 1;
      isHave = true;
    }

    // 去请求服务器数据.
    Map<String, dynamic> resultMap = await DioHttpManager()
        .request(MyApi.getObjectTechnician,
        method: DioHttpManager.post, data: {"page": page, "limit": limit, "template_id": tId});

    page += 1;

    if (resultMap['code'] == 0) {
      List<dynamic> resultList = resultMap["data"] ?? [];
      if (resultList.length < limit) {
        //小于分页数据, 就没有更多数据了
        isHave = false;
      } else {
        isHave = true;
      }

      for (var element in resultList) {
        dataList.add(NearbyTechnicianModel.fromMap(element));
      }
      // 更新页面.
      if (dataList.isEmpty) {
        setState(ViewState.empty);
      } else {
        setState(ViewState.normal);
      }
    } else {
      // 更新页面.
      setState(ViewState.error);
    }
  }
}
